using System;
using System.Text;

namespace SharpObjects.Cryptography.DigitalSignature
{
	/// <summary>
	/// Summary description for Encryptor.
	/// </summary>
	public class NotaryPublic
	{
		protected IDigitalSignatureProvider m_provider = null;

		public NotaryPublic(IDigitalSignatureProvider provider)
		{
			m_provider = provider;
		}




		public byte [] GetSignature(string sUtf8Data)
		{
			return GetSignature(Encoding.UTF8.GetBytes(sUtf8Data));
		}

		public byte [] GetSignature(string sData, System.Text.Encoding encoding)
		{
			return GetSignature(encoding.GetBytes(sData));
		}

		public byte [] GetSignature(byte [] data)
		{
			return m_provider.Sign(m_provider.GetHash(data));
		}

		public string GetBase64Signature(string sUtf8Data)
		{
			return GetBase64Signature(Encoding.UTF8.GetBytes(sUtf8Data));
		}

		public string GetBase64Signature(string sData, System.Text.Encoding encoding)
		{
			return GetBase64Signature(encoding.GetBytes(sData));
		}

		public string GetBase64Signature(byte [] data)
		{
			byte [] signature = m_provider.Sign(m_provider.GetHash(data));
			return Convert.ToBase64String(signature);
		}

		public string GetNumberStringSignature(string sUtf8Data)
		{
			return GetNumberStringSignature(Encoding.UTF8.GetBytes(sUtf8Data));
		}

		public string GetNumberStringSignature(string sData, System.Text.Encoding encoding)
		{
			return GetNumberStringSignature(encoding.GetBytes(sData));
		}

		public string GetNumberStringSignature(byte [] data)
		{
			byte [] signature = m_provider.Sign(m_provider.GetHash(data));
			return ConvertToNumberString(signature);
		}

		public string ConvertToNumberString(byte [] signature)
		{
			StringBuilder sb = new StringBuilder();

			for(int k = 0; k < signature.Length; k++)
			{
				if(k == 0)
				{
					sb.Append(signature[k].ToString());
				}
				else
				{
					sb.AppendFormat(",{0}", signature[k].ToString());
				}
			}

			return sb.ToString();
		}

		public byte [] ConvertFromNumberString(string sSignature)
		{
			string [] numbers = sSignature.Split(',');
			byte [] signature = new byte[numbers.Length];

			for(int k = 0; k < numbers.Length; k++)
			{
				signature[k] = Convert.ToByte(numbers[k], 10);
			}

			return signature;
		}

		
		public bool Verify(byte [] data, byte [] signature)
		{
			byte [] hash = m_provider.GetHash(data);
			return m_provider.Verify(hash, signature);
		}

		public bool Verify(string sUtf8Data, byte [] signature)
		{
			byte [] data = Encoding.UTF8.GetBytes(sUtf8Data);
			return Verify(data, signature);
		}

		public bool Verify(string sData, System.Text.Encoding encoding, byte [] signature)
		{
			byte [] data = encoding.GetBytes(sData);
			return Verify(data, signature);
		}

		public bool VerifyBase64(string sUtf8Data, string sBase64Signature)
		{
			byte [] data = Encoding.UTF8.GetBytes(sUtf8Data);
			byte [] signature = Convert.FromBase64String(sBase64Signature);
			return Verify(data, signature);
		}

		public bool VerifyBase64(string sData, System.Text.Encoding encoding, string sBase64Signature)
		{
			byte [] data = encoding.GetBytes(sData);
			byte [] signature = Convert.FromBase64String(sBase64Signature);
			return Verify(data, signature);
		}

		public bool VerifyNumberString(string sUtf8Data, string sNumberStringSignature)
		{
			byte [] data = Encoding.UTF8.GetBytes(sUtf8Data);
			byte [] signature = ConvertFromNumberString(sNumberStringSignature);
			return Verify(data, signature);
		}

		public bool VerifyNumberString(string sData, System.Text.Encoding encoding, string sNumberStringSignature)
		{
			byte [] data = encoding.GetBytes(sData);
			byte [] signature = ConvertFromNumberString(sNumberStringSignature);
			return Verify(data, signature);
		}





	}
}
